﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.BaseSystem;
using BF.Model;
using Base.Core.UnityFactory;
using BF.Infrastructure;
using WebDataControls;

namespace BF.Web.pages.BaseSystem.FunctionManagement
{
    public partial class FunctionEdit : BasePage
    {
        private readonly Isys_FunctionBLL iBLL = IOCFactory.GetIOCResolve<Isys_FunctionBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageControl();
            }
        }

        /// <summary>
        /// 加载页面控制
        /// </summary>
        private void PageControl()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ParentID"]))//新增操作
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ParentLevel"]))
                {
                    this.iLevel.Text = (Convert
                        .ToInt32(Request.QueryString["ParentLevel"]) + 1)
                        .ToString();
                }
                this.iType.Items.Clear();
                if (Request.QueryString["ParentType"] == "1")
                {
                    this.iType.Items.Add(new ListItem("目录", "1"));
                    this.iType.Items.Add(new ListItem("页面", "2"));
                }
                else if (Request.QueryString["ParentType"] == "2")
                {
                    this.iType.Items.Add(new ListItem("操作", "3"));
                }
            }
            else//修改操作
            {
                sys_Function model = iBLL.GetModelByID(Request.QueryString["ID"]);
                WebUIHelper.SetControlsValue(model, "sys_Function", this.Form);
                this.hiddenParentID.Value = model.gParentKey.ToString();
                this.hiddenParentType.Value = (Convert
                    .ToInt32(model.iType.ToString()) - 1)
                    .ToString();
                this.iIsCanUse.Checked = model.bFlag;
                this.bIsShow.Checked = model.bIsShow;
                this.dropIconCls.SelectedValue = model.sIconCls;
                this.iType.Items.Clear();
                this.iType.Items.Add(new ListItem(GetTypeString(model.iType),
                    model.iType.ToString()));
            }
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            sys_Function model = GetMode();
            if (!string.IsNullOrEmpty(Request.QueryString["ParentID"]))//新增操作
            {

                if (iBLL.Add(model))
                {
                    Messages("新增成功!",
                        @"function(result) {
                            winer.RefurbishParent(); 
                            winer.win.setTitle('修改系统功能模块'); 
                            window.location.href='FunctionEdit.aspx?ID="
                        + model.gKey
                        + "'}",
                        Updatepanel1);
                }
                else
                {
                    Messages("新增失败!");
                }
            }
            else//修改操作
            {
                if (iBLL.Update())
                {
                    Messages("修改成功!", "function(e){winer.RefurbishParent(); }", this);
                }
                else
                {
                    Messages("修改失败!");
                }
            }
        }


        ///<summary>
        ///实体赋值
        ///</summary>
        ///<returns></returns>
        private sys_Function GetMode()
        {
            sys_Function model = null;

            if (!string.IsNullOrEmpty(Request.QueryString["ParentID"]))//新增操作
            {
                model = new sys_Function();
                model.gKey = Guid.NewGuid();
                model.gParentKey = new Guid(Request.QueryString["ParentID"]);
                int? sort = iBLL
                    .GetList(p => p.gParentKey == model.gParentKey.Value)
                    .Max(P => P.iSort);  //model.iSort
                model.iSort = (sort ?? -1) + 1;//顺序
            }
            else//修改操作 
            {
                model = iBLL.GetModelByID(Request.QueryString["ID"]);
            }
            model = (sys_Function)WebUIHelper.GetControlsModel(model, 
                "sys_Function", this.Form);
            model.iLevel = Convert.ToInt32(this.iLevel.Text);//目录级别
            model.iType = Convert.ToInt32(this.iType.SelectedValue);//目录类别
            model.sIconCls = dropIconCls.SelectedValue.ToString().Trim();

            model.bIsRoot = false;
            model.bFlag = this.iIsCanUse.Checked;
            model.bIsShow = this.bIsShow.Checked;
            return model;
        }
        /// <summary>
        /// 根据类型的ID获取名称
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        private string GetTypeString(int typeid)
        {
            switch (typeid)
            {
                case 1:
                    return "目录";
                case 2:
                    return "页面";
                case 3:
                    return "操作";
                default:
                    return "";
            }
        }
    }
}